/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
GroupWindow = function() {
	this.group;
    this.groupName = new Ext.form.TextField({
        fieldLabel: 'Group Name',
        emptyText: '',
        width: 314,
        validationEvent: false,
        validateOnBlur: false,
        msgTarget: 'under',
        triggerAction: 'all',
        displayField: 'url',
        mode: 'local',
        listeners:{scope: this}
    });

    this.groupDesc = new Ext.form.TextArea({
        fieldLabel: 'Group Description',
        emptyText: '',
        width: 314,
        validationEvent: false,
        validateOnBlur: false,
        msgTarget: 'under',
        triggerAction: 'all',
        displayField: 'url',
        mode: 'local',
        listeners:{scope: this}
    });

    this.form = new Ext.FormPanel({
        labelAlign:'top',
        items: [this.groupName,this.groupDesc],
        border: false,
        bodyStyle:'background:transparent;padding:10px;'
    });

	GroupWindow.superclass.constructor.call(this, {
        title: 'Create New Group',
        iconCls: 'group-icon',
        autoHeight: true,
        width: 350,
        resizable: false,
        plain:true,
        modal: true,
        y: 100,
        autoScroll: false,
        closeAction: 'hide',

        buttons:[{
            text: 'Create',
            handler: this.onCreate,
            scope: this
        },{
            text: 'Save',
            handler: this.onSave,
            scope: this
        },{
            text: 'Cancel',
            handler: this.hide.createDelegate(this, [])
        }],

        items: this.form
    });
}

Ext.extend(GroupWindow, Ext.Window, {
    
    show : function(group){
        this.group = group;
        
        if(this.group) {
        	this.setTitle('Edit Group');
        	this.groupName.setValue(this.group.name);
        	this.groupDesc.setValue(this.group.description);
        	this.buttons[0].hide();
        	this.buttons[1].show();
        } else {
        	this.setTitle('Create New Group');
        	this.groupName.setValue('');
        	this.groupDesc.setValue('');
        	this.buttons[1].hide();
        	this.buttons[0].show();
        }

        GroupWindow.superclass.show.apply(this, arguments);
    },

    onCreate: function() {
        if(this.groupName.getValue() == '')
       	{
       		this.groupName.markInvalid("Invalid group name!");
       	} else { 
	        this.el.mask('Creating New Group...', 'x-mask-loading');
	       	SGui.createGroup(this.groupName.getValue(), this.groupDesc.getValue(), this.validateGroup, this);
       	}
    },

    onSave: function() {
		if(this.groupName.getValue() == '')
       	{
       		this.groupName.markInvalid("Invalid group name!");
       	} else { 
	        this.el.mask('Saving Group...', 'x-mask-loading');
	       	SGui.saveGroup(this.group.id,this.groupName.getValue(), this.groupDesc.getValue(), this.validateGroup, this);
       	}
    },

    markInvalid : function(msg){
        this.groupName.markInvalid(msg);
        this.el.unmask();
    },

    validateGroup: function(obj,result){
    	if(result.length > 1) {
    		if(result[0] == '1') {
                obj.el.unmask();
                obj.hide();
   				return obj.fireEvent('validgroup', result[1]);
    		} else {
    			obj.markInvalid(result[1]);
    		}    		
    	} else {
    		alert('Invalid Protocol From Server...');
    	}
	}
});
